package com.example.mywiseelves.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.mywiseelves.R;
import com.example.mywiseelves.view.ExitView;
import com.example.mywiseelves.view.dialog.MyDialog;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link ExitFragment#newInstance} factory method to
 * create an instance of this fragment.
 * 退出界面
 */
public class ExitFragment extends Fragment implements View.OnClickListener {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    //水晶控件
    private ExitView exitView;
    //退出提示弹出框
    private MyDialog myExitDialog;
    //获取布局
    View view;
    public ExitFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment ExitFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static ExitFragment newInstance(String param1, String param2) {
        ExitFragment fragment = new ExitFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view=inflater.inflate(R.layout.fragment_exit, container, false);
        findViews();
        //传送水晶，事件监听器
        exitView.setOnClickListener(this);
        return view;
    }

    /**
     * 实例化控件
     * */
    private void findViews() {
        exitView=view.findViewById(R.id.exit_ev);
    }


    /**
     * 实现点击事件
     * */
    @Override
    public void onClick(View v) {
        openExitDialog();
    }

    /**
     * 开启退出弹窗
     * */
    public void openExitDialog(){
        myExitDialog =new MyDialog(getActivity(),R.style.ExitDialog);
        myExitDialog.setTitle("传送水晶");
        //设置内容
        myExitDialog.setMessage("我是传送水晶，您确定打算离开这里吗？");

        myExitDialog.setYesOnclickListener("是的", new MyDialog.onYesOnclickListener() {
            @Override
            public void onYesOnclick() {
                Toast.makeText(getContext(),"好的，传送水晶这就帮您传送！", Toast.LENGTH_LONG).show();
                getActivity().finish();
                myExitDialog.dismiss();
            }
        });
        myExitDialog.setNoOnclickListener("不，等会", new MyDialog.onNoOnclickListener() {
            @Override
            public void onNoClick() {
                Toast.makeText(getContext(),"好的，传送水晶明白了！", Toast.LENGTH_LONG).show();
                myExitDialog.dismiss();
            }
        });
        myExitDialog.show();
    }
}